//
//  LeftDrawerViewController.m
//  SoutheastAsia
//
//  Created by rimi on 16/1/9.
//  Copyright © 2016年 zlj. All rights reserved.
//

#import "LeftDrawerViewController.h"
#import "LeftDrawerButton.h"
#import "LoginViewController.h"
#import "UserModel.h"
#import "ChangePasswordViewController.h"
#import "CollectionViewController.h"
#import "CustomLoginViewController.h"

#define SCREEN_WIDTH CGRectGetWidth(self.view.frame)
#define SCREEN_HEIGHT CGRectGetHeight(self.view.frame)
#define LEFTDRAWERBUTTON_TAG 40
@interface LeftDrawerViewController ()

@property (nonatomic, strong) UIImageView *leftDrawerBackground;
@property (nonatomic, strong) UILabel *welcomeLabel;
@property (nonatomic, strong) UILabel *nameLabel;
@property (nonatomic, strong) LeftDrawerButton *loginButton;
@property (nonatomic, strong) LeftDrawerButton *changePasswordButton;
@property (nonatomic, strong) LeftDrawerButton *myCollectionButton;

- (void)initializeAppearance;
@end

@implementation LeftDrawerViewController

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    self.nameLabel.text = [UserModel defaultUesr].userName;
   
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    NSString *isLogin = [userDefaults objectForKey:@"isLogin"];
    NSString *userName = [userDefaults objectForKey:@"userName"];
    if (![isLogin isEqualToString:@"yes"]) {
        [self.loginButton setTitle:@"登陆" forState:UIControlStateNormal];
        [self.loginButton setImage:[UIImage imageNamed:@"登录前.png"] forState:UIControlStateNormal];
    } else {
        [self.loginButton setTitle:@"注销" forState:UIControlStateNormal];
        [self.loginButton setImage:[UIImage imageNamed:@"登录后.png"] forState:UIControlStateNormal];
        self.nameLabel.text = userName;
}
    
//    if ([[UserModel defaultUesr].userName isEqualToString:@""]) {
//        [self.loginButton setTitle:@"登陆" forState:UIControlStateNormal];
//        [self.loginButton setImage:[UIImage imageNamed:@"登录前.png"] forState:UIControlStateNormal];
//    } else {
//        [self.loginButton setTitle:@"注销" forState:UIControlStateNormal];
//                [self.loginButton setImage:[UIImage imageNamed:@"登录后.png"] forState:UIControlStateNormal];
//    }
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initializeAppearance];
    
}
#pragma mark - private methods
- (void)initializeAppearance {
    [self.view addSubview:self.leftDrawerBackground];
    [self.view addSubview:self.welcomeLabel];
    [self.view addSubview:self.nameLabel];
    
    [self.view addSubview:self.loginButton];
    [self.view addSubview:self.myCollectionButton];
    [self.view addSubview:self.changePasswordButton];
}

#pragma mark - callback/action
- (void)controlOfLeftDrawerButton:(UIButton *)sender {
    switch (sender.tag - LEFTDRAWERBUTTON_TAG) {
        case 0://登陆
        {
            if ([self.loginButton.titleLabel.text isEqualToString:@"登陆"]) {
                CustomLoginViewController *loginVC = [[CustomLoginViewController alloc] init];
                UINavigationController *loginNC = [[UINavigationController alloc] initWithRootViewController:loginVC];
                loginNC.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
                loginVC.navigationController.navigationBarHidden = YES;
                [self presentViewController:loginNC animated:YES completion:nil];
            } else {
                
                UIAlertController *alertAction = [UIAlertController alertControllerWithTitle:@"注销成功" message:@"欢迎下次登陆" preferredStyle:UIAlertControllerStyleAlert];
                
                UIAlertAction *exitAlert = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:nil];
                
                [alertAction addAction:exitAlert];
                [self presentViewController:alertAction animated:YES completion:nil];
                
                [sender setTitle:@"登陆" forState:UIControlStateNormal];
                [sender setImage:[UIImage imageNamed:@"登录前"] forState:UIControlStateNormal];
                [UserModel defaultUesr].userName = @"";
                self.nameLabel.text = [UserModel defaultUesr].userName;
                
                // 登陆状态
                NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
                [userDefaults setObject:@"no" forKey:@"isLogin"];
                [userDefaults setObject:@"" forKey:@"userName"];
                [userDefaults synchronize];
            }
            
        }
            break;
        case 1://我的收藏
        {
            if ([self.loginButton.titleLabel.text isEqualToString:@"注销"]) {
                CollectionViewController *CollectionVC = [[CollectionViewController alloc] init];
                [self.navigationController pushViewController:CollectionVC animated:YES];
            } else {
                UIAlertController *alertAction = [UIAlertController alertControllerWithTitle:@"请您先登陆" message:@"" preferredStyle:UIAlertControllerStyleAlert];
                
                UIAlertAction *exitAlert = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:nil];
                
                [alertAction addAction:exitAlert];
                [self presentViewController:alertAction animated:YES completion:nil];
            }
        }
            break;
        case 2://修改密码
        {
            if ([self.loginButton.titleLabel.text isEqualToString:@"注销"]) {
                ChangePasswordViewController *changeVC = [[ChangePasswordViewController alloc] init];
                [self.navigationController pushViewController:changeVC animated:YES];
            } else {
                UIAlertController *alertAction = [UIAlertController alertControllerWithTitle:@"请您先登陆" message:@"" preferredStyle:UIAlertControllerStyleAlert];
                
                UIAlertAction *exitAlert = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:nil];
                
                [alertAction addAction:exitAlert];
                [self presentViewController:alertAction animated:YES completion:nil];
            }
        }
            
        default:
            break;
    }
}
#pragma mark - getter
- (UILabel *)welcomeLabel {
    if (!_welcomeLabel) {
        _welcomeLabel = [[UILabel alloc] initWithFrame:CGRectMake(60, 100, 160, 60)];
        _welcomeLabel.text = @"欢迎光临";
        [_welcomeLabel setFont:[UIFont systemFontOfSize:26 weight:10]];
        [_welcomeLabel setTextColor:[UIColor cyanColor]];
    }
    return _welcomeLabel;
}

- (UIImageView *)leftDrawerBackground {
    if (!_leftDrawerBackground) {
        _leftDrawerBackground = ({
            UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"抽屉背景.png"]];
            imageView.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
            imageView;
        });
    }
    return _leftDrawerBackground;
}

- (UILabel *)nameLabel {
    if (!_nameLabel) {
        _nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(130, 140, 140, 40)];
        _nameLabel.text = @"";
        [_nameLabel setFont:[UIFont systemFontOfSize:18 weight:4]];
        [_nameLabel setTextColor:[UIColor colorWithRed:1.000 green:0.000 blue:0.035 alpha:1.000]];
    }
    return _nameLabel;
}
- (LeftDrawerButton *)loginButton {
    if (!_loginButton) {
        _loginButton = ({
            LeftDrawerButton *button = [LeftDrawerButton buttonWithType:UIButtonTypeCustom];
            [button setImage:[UIImage imageNamed:[NSString stringWithFormat:@"登陆前.png"]] forState:UIControlStateNormal];
            [button setTitle:@"登陆" forState:UIControlStateNormal];
            [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
            [button.titleLabel setFont:[UIFont systemFontOfSize:24 weight:1]];
            [button.titleLabel setTextAlignment:NSTextAlignmentCenter];
            button.frame = CGRectMake(40, 220, 160, 40);
            button.tag = LEFTDRAWERBUTTON_TAG;
            [button addTarget:self action:@selector(controlOfLeftDrawerButton:) forControlEvents:UIControlEventTouchUpInside];
            button;
        });
    }
    return _loginButton;
}

- (LeftDrawerButton *)changePasswordButton {
    if (!_changePasswordButton) {
        _changePasswordButton = ({
            LeftDrawerButton *button = [LeftDrawerButton buttonWithType:UIButtonTypeCustom];
            [button setImage:[UIImage imageNamed:[NSString stringWithFormat:@"修改密码.png"]] forState:UIControlStateNormal];
            [button setTitle:@"修改密码" forState:UIControlStateNormal];
            [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
            [button.titleLabel setFont:[UIFont systemFontOfSize:24 weight:1]];
            [button.titleLabel setTextAlignment:NSTextAlignmentCenter];
            button.frame = CGRectMake(40, 420, 160, 40);
            button.tag = LEFTDRAWERBUTTON_TAG + 2;
            [button addTarget:self action:@selector(controlOfLeftDrawerButton:) forControlEvents:UIControlEventTouchUpInside];
            button;
        });
    }
    return _changePasswordButton;
}
- (LeftDrawerButton *)myCollectionButton {
    if (!_myCollectionButton) {
        _myCollectionButton = ({
            LeftDrawerButton *button = [LeftDrawerButton buttonWithType:UIButtonTypeCustom];
            [button setImage:[UIImage imageNamed:[NSString stringWithFormat:@"我的收藏.png"]] forState:UIControlStateNormal];
            [button setTitle:@"我的收藏" forState:UIControlStateNormal];
            [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
            [button.titleLabel setFont:[UIFont systemFontOfSize:24 weight:1]];
            [button.titleLabel setTextAlignment:NSTextAlignmentCenter];
            button.frame = CGRectMake(40, 320, 160, 40);
            button.tag = LEFTDRAWERBUTTON_TAG + 1;
            [button addTarget:self action:@selector(controlOfLeftDrawerButton:) forControlEvents:UIControlEventTouchUpInside];
            button;
        });
    }
    return _myCollectionButton;
}
@end
